
package rentalgui;

import database.ReportDao;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import javax.swing.SpinnerDateModel;

public class ReportMainWindow extends javax.swing.JFrame {

    /**
     * Creates new form ExpenseWindow
     */
    public ReportMainWindow(ReportDao rdao) {
        initComponents();
        this.rdao = rdao;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        textDisplay = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        inputFromDate = new javax.swing.JSpinner();
        jLabel2 = new javax.swing.JLabel();
        inputToDate = new javax.swing.JSpinner();
        buttonTotal = new javax.swing.JButton();
        buttonByProperty = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Rental Database - Lease");

        textDisplay.setColumns(20);
        textDisplay.setEditable(false);
        textDisplay.setLineWrap(true);
        textDisplay.setRows(5);
        jScrollPane2.setViewportView(textDisplay);

        jLabel1.setText("From date:");

        inputFromDate.setModel(new javax.swing.SpinnerDateModel(new java.util.Date(1336701669244L), null, null, java.util.Calendar.DAY_OF_MONTH));
        inputFromDate.setEditor(new javax.swing.JSpinner.DateEditor(inputFromDate, "dd/MM/yyyy"));

        jLabel2.setText("To date:");

        inputToDate.setModel(new javax.swing.SpinnerDateModel(new java.util.Date(1336701669244L), null, null, java.util.Calendar.DAY_OF_MONTH));
        inputToDate.setEditor(new javax.swing.JSpinner.DateEditor(inputToDate, "dd/MM/yyyy"));

        buttonTotal.setText("Total Expense/Cost Report");
        buttonTotal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonTotalActionPerformed(evt);
            }
        });

        buttonByProperty.setText("Expense/Cost by Property");
        buttonByProperty.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonByPropertyActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(buttonTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(inputFromDate, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(inputToDate, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(buttonByProperty, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 10, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane2)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(inputFromDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(inputToDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonByProperty)
                    .addComponent(buttonTotal))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 223, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buttonTotalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonTotalActionPerformed
        SpinnerDateModel fromModel = (SpinnerDateModel)inputFromDate.getModel();
        Date fromDate = fromModel.getDate();
        Calendar fromCalendar = Calendar.getInstance();
        fromCalendar.setTime(fromDate);
        SpinnerDateModel toModel = (SpinnerDateModel)inputFromDate.getModel();        
        Date toDate = toModel.getDate();
        Calendar toCalendar = Calendar.getInstance();
        toCalendar.setTime(toDate);
        String result = rdao.getExpenseCostReport(fromCalendar, toCalendar);
        textDisplay.setText(result);
    }//GEN-LAST:event_buttonTotalActionPerformed

    private void buttonByPropertyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonByPropertyActionPerformed
        SpinnerDateModel fromModel = (SpinnerDateModel)inputFromDate.getModel();
        Date fromDate = fromModel.getDate();
        Calendar fromCalendar = Calendar.getInstance();
        fromCalendar.setTime(fromDate);
        SpinnerDateModel toModel = (SpinnerDateModel)inputFromDate.getModel();        
        Date toDate = toModel.getDate();
        Calendar toCalendar = Calendar.getInstance();
        toCalendar.setTime(toDate);
        ArrayList<String> readout = new ArrayList<String>();
        try{
            readout = (ArrayList<String>)rdao.getExpenseCostReportByProperty(fromCalendar, toCalendar);
        }
        catch(Exception e){
            
        }
        String result = "";
        for(String s : readout)
            result += s + "\n\n";
        textDisplay.setText(result);
    }//GEN-LAST:event_buttonByPropertyActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonByProperty;
    private javax.swing.JButton buttonTotal;
    private javax.swing.JSpinner inputFromDate;
    private javax.swing.JSpinner inputToDate;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea textDisplay;
    // End of variables declaration//GEN-END:variables
    private ReportDao rdao;
}
